package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import entity.User;
import mapper.UserMapper;
import util.MybatisUtil;

import java.util.List;

/**
 * 本类用来演示: 接口的实现类
 *
 * service层调用mapper层
 *
 * @author: success
 * @date: 2022/10/22 11:18 上午
 */
public class UserServiceImpl implements UserService{
    @Override
    public List<User> findAll() {
        //1. 获取sqlSession
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        //2. 获取userMapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //3. mapper接口中的方法
        List<User> userList = userMapper.findAll();

        //TODO... 可能需要对mapper取出来的数据进行进一步的业务逻辑上的处理

        //4. 关闭资源
        sqlSession.close();

        return userList; // 返回的是数据处理的结果
    }

    @Override
    public User getById(Integer id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.getById(id);

        sqlSession.close();
        return user;
    }

    @Override
    public User login(String username) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.getByUsername(username);

        sqlSession.close();

        return user;
    }

    @Override
    public Boolean save(User user) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int num=userMapper.save(user);
        sqlSession.commit();
        sqlSession.close();
        return num>0;
    }

    @Override
    public List<User> getByUsername(String username) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList=userMapper.findByLikeUsername(username);
        sqlSession.close();
        return userList;
    }

    @Override
    public boolean deleteUser(Integer userId) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int rows = userMapper.delById(userId);
        sqlSession.commit();
        sqlSession.close();
        return rows > 0;
    }

    @Override
    public boolean updateUser(User user) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int rows =  userMapper.update(user);
        sqlSession.commit(); // 事务提交
        sqlSession.close();
        return rows > 0;
    }

    @Override
    public PageInfo<User> getByPage(Integer pageNum) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // PageHelper 插件 拦截器interceptor,
        //1. 拦截的业务： 查看他的下一行代码 是不是select , 获得到数据库的类型（分页SQL语句不一样）
        //2. 在原有的select  SQL后，拼接分页sql  ( select * from t_user limit m,n)
        PageHelper.startPage(pageNum,3);
        List<User> userList =  userMapper.findAll(); // select * from t_user

        // PageInfo： (1) 页码的信息  (2)数据的信息
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }
}
